﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Morse
{
    public class MorseEncode
    {
        private static
            Dictionary<char, string> morseCode = new Dictionary<char, string>(){
                {'A', "*-"},
                {'B', "-***"},
                {'C', "-*-*"},
                {'D', "-**"},
                {'E', "*"},
                {'F', "**-*"},
                {'G', "--*"},
                {'H', "****"},
                {'I', "**"},
                {'J', "*---"},
                {'K', "-*-"},
                {'L', "*-**"},
                {'M', "--"},
                {'N', "-*"},
                {'O', "---"},
                {'P', "*--*"},
                {'Q', "--*-"},
                {'R', "*-*"},
                {'S', "***"},
                {'T', "-"},
                {'U', "**-"},
                {'V', "***-"},
                {'W', "*--"},
                {'X', "-**-"},
                {'Y', "-*--"},
                {'Z', "--**"},
                {'0',"-----"},
                {'1',"*----"},
                {'2',"**---"},
                {'3',"***--"},
                {'4',"****-"},
                {'5',"*****"},
                {'6',"-****"},
                {'7',"--***"},
                {'8',"---**"},
                {'9', "----*"},
                {' ', "       "}
            };
        public static string Encode(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;
            //input = CHS2PinYin.Convert(input);
            input = input.ToUpper();
            var sb = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                if (morseCode.ContainsKey(input[i]))
                {
                    sb.Append(morseCode[input[i]]);
                    if (input[i] != ' ')
                        sb.Append("   ");
                }
            }

            return sb.ToString();

        }

        public static List<IPlayStep> GetPlaySteps(string code)
        {
            var codes = code.ToCharArray();
            var steps = new List<IPlayStep>();
            for (int i = 0; i < codes.Length; i++)
            {
                IPlayStep s;
                switch (codes[i])
                {
                    case '*':
                        s = new Dit();
                        break;
                    case '-':
                        s = new Dah();
                        break;
                    default:
                        s = new Empty();
                        break;
                }

                steps.Add(s);
                if (!(s is Empty))
                {
                    steps.Add(new Empty());
                }
            }
            return steps;
        }
    }
}
